package approve.action;

import java.util.List;

import nguyen.dao.ReceiverDocumentDAO;

import model.pojo.Document;
import model.pojo.Employee;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Re_DeclineDocument extends Activity{
	String database = "admin";
	Bundle language=null;
	String lang = "EN";
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
			lang=extras.getString("lang");
			language=extras.getBundle(lang);
		}
		return database;
	}
	
	Employee emp=null;
	
	ListView list;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.re_declinedocument);   
        getDatabase();
        
        //saved-document,metting_pending,my-document,new-document,metting_sending,not-approved-document,
        //approved-document,reference_document,pending-document,document_sending
        emp=(Employee) getIntent().getExtras().get("em");
        List<Document> listDocs=getListDeclineDocument();
        if(language!=null){
        	TextView la_headerTitle=(TextView)findViewById(R.id.la_headerTitle);
        	la_headerTitle.setText(language.getString("declined-document")+" ("+listDocs.size()+"):");
        }
        //System.out.println("declinedoc: "+(listDocs.size()>0));
        if(listDocs.size()>0){
        	list=(ListView) findViewById(R.id.declinedocumentlist);
            list.setAdapter(new ArrayAdapter<Document>(this, android.R.layout.simple_list_item_1,listDocs));
            list.setOnItemClickListener(viewDocument);
        }else{
        	TextView empltyList=(TextView) findViewById(R.id.empltyList);
			if(language!=null){
				empltyList.setText(language.getString("no_data"));
		}
        }
        
    }
	
	private OnItemClickListener viewDocument=new OnItemClickListener() {


		public void onItemClick(AdapterView<?> listDoc, View arg1, int arg2,
				long arg3) {
			Document doc=(Document) listDoc.getItemAtPosition(arg2);
			
			if(doc.getTypeDoc()<11){
				Intent intent=new Intent(Re_DeclineDocument.this,Re_ViewForm_LessThan10.class);				
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}
			else if(doc.getTypeDoc()==12){
				Intent intent=new Intent(Re_DeclineDocument.this,Re_ViewForm12.class);				
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}
			else if(doc.getTypeDoc()==13){
				Intent intent=new Intent(Re_DeclineDocument.this,Re_ViewForm13.class);				
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}
			else if(doc.getTypeDoc()==15){
				Intent intent=new Intent(Re_DeclineDocument.this,Re_ViewForm15.class);				
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}else if (doc.getTypeDoc() == 16) {
				Intent intent = new Intent(Re_DeclineDocument.this,
						Re_ViewForm16.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}
			else{
				Intent intent=new Intent(Re_DeclineDocument.this,Re_ViewForm14.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}			
		}
		
	};

	private List<Document> getListDeclineDocument(){
		return ReceiverDocumentDAO.getDeclineDocument(emp.getIdemployee(), database,lang);
	}
//	private List<Document> getListDeclineDocument(){
//		String sql="SELECT approvedoc.* FROM approvedoc,listapprove WHERE IDTypeDoc != 11 and listapprove.IDEmployee='1322887525530'" +
//					" AND listapprove.IDGroupApprove = approvedoc.IDGroupApprove AND listapprove.IDStatus=3 " +
//								"AND approvedoc.IDStatus=3";
//		String host="14.63.219.99";
//		String database="elihan";
//		String url="jdbc:mysql://"+host+":3306/"+database;
//		String user="erp";
//		String password="nguyen";
//		
//		Connection con=null;
//		
//		List<Document> list=new ArrayList<Document>();
//		try {
//			DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
//			con=(Connection) DriverManager.getConnection(url, user, password);
//			Statement statement= (Statement) con.createStatement();
//			ResultSet rs= statement.executeQuery(sql);			
//			Document doc=null;
//			while(rs.next()){	
//				doc=new Document(rs.getInt("IDApproveDoc"),rs.getString("Tile"),rs.getString("Description"),rs.getString("Content"));
//				list.add(doc);
//		
//			}
//		} catch (SQLException e) {	
//			openDialog();
//			System.out.println("ERROR CONNECT");
//			e.printStackTrace();
//		}
//		finally{
//			try {
//				con.close();
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
//		}
//		return list;
//	
//	}

	private void openDialog() {
		// TODO Auto-generated method stub
		
	}
}
